package com.ruoyi.web.controller.personnel;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.personnel.domain.ResearchProject;
import com.ruoyi.personnel.service.IResearchProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 科研项目Controller
 *
 * @author ruoyi
 * @date 2021-03-01
 */
@Api("科研项目")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/scientific/project" )
public class ResearchProjectController extends BaseController {

    private final IResearchProjectService iResearchProjectService;

    /**
     * 查询科研项目列表
     */
    @ApiOperation("查询科研项目列表")
    @PreAuthorize("@ss.hasPermi('scientific:project:list')")
    @GetMapping("/list")
    public TableDataInfo list(ResearchProject researchProject) {
        startPage();
        List<ResearchProject> list = iResearchProjectService.queryList(researchProject);
        return getDataTable(list);
    }

    /**
     * 导出科研项目列表
     */
    @ApiOperation("导出科研项目列表")
    @PreAuthorize("@ss.hasPermi('scientific:project:export')" )
    @Log(title = "科研项目" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(ResearchProject researchProject) {
        List<ResearchProject> list = iResearchProjectService.queryList(researchProject);
        ExcelUtil<ResearchProject> util = new ExcelUtil<ResearchProject>(ResearchProject.class);
        return util.exportExcel(list, "project" );
    }

    /**
     * 获取科研项目详细信息
     */
    @ApiOperation("获取科研项目详细信息")
    @PreAuthorize("@ss.hasPermi('scientific:project:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) String id) {
        return AjaxResult.success(iResearchProjectService.getById(id));
    }

    /**
     * 新增科研项目
     */
    @ApiOperation("新增科研项目")
    @PreAuthorize("@ss.hasPermi('scientific:project:add')" )
    @Log(title = "科研项目" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ResearchProject researchProject) {
        researchProject.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iResearchProjectService.save(researchProject) ? 1 : 0);
    }

    /**
     * 修改科研项目
     */
    @ApiOperation("修改科研项目")
    @PreAuthorize("@ss.hasPermi('scientific:project:edit')" )
    @Log(title = "科研项目" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ResearchProject researchProject) {
        return toAjax(iResearchProjectService.updateById(researchProject) ? 1 : 0);
    }

    /**
     * 删除科研项目
     */
    @ApiOperation("删除科研项目")
    @PreAuthorize("@ss.hasPermi('scientific:project:remove')" )
    @Log(title = "科研项目" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(iResearchProjectService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
